import 'package:base_library/base_library.dart';
import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart';
import 'package:wan_android_flutter/protocol/model.dart';
import 'package:wan_android_flutter/repository/user_repository.dart';
import 'package:wan_android_flutter/res/Ids.dart';
import 'package:wan_android_flutter/ui/pages/users/register.dart';
import 'package:wan_android_flutter/utils/navigator_util.dart';

class Login extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      resizeToAvoidBottomInset: false,
      body: Stack(
        children: <Widget>[
          Image.asset(
            Util.getImgPath('ic_login_bg'),
            package: BaseConstant.packageBase,
            width: double.infinity,
            height: double.infinity,
            fit: BoxFit.cover,
          ),
          LoginBody()
        ],
      ),
    );
  }
}

class LoginBody extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    TextEditingController _loginNameController = TextEditingController();
    TextEditingController _pwdNameController = TextEditingController();
    UserRepository repository = UserRepository();
    void _goLogin() {
      if (_loginNameController.text.length <= 0) {
        Util.showSnackBar(context, '请输入账号');
      }
      if (_pwdNameController.text.length <= 0) {
        Util.showSnackBar(context, '请输入密码');
      }

      LoginReq loginReq =
          LoginReq(_loginNameController.text, _pwdNameController.text);
      repository.login(loginReq).then((UserModel userModel) {
        Util.showSnackBar(context, '登录成功');
        Observable.just(1).delay(new Duration(milliseconds: 500)).listen((_) {
          RouteUtil.goMain(context);
        });
      }).catchError((e) {
        LogUtil.e("LoginResp error: ${e.toString()}");
        Util.showSnackBar(context, e.toString());
      });
    }

    return Column(
      children: <Widget>[
        Expanded(
          child: Container(),
        ),
        Expanded(
            child: Container(
                margin: EdgeInsets.only(left: 20, top: 15, right: 20),
                child: Column(
                  children: <Widget>[
                    LoginItem(
                      prefixIcon: Icons.person,
                      hintText: '请输入用户名',
                      controller: _loginNameController,
                    ),
                    Gaps.vGap15,
                    LoginItem(
                      prefixIcon: Icons.lock,
                      hintText: '请输入密码',
                      hasSuffixIcon: true,
                      controller: _pwdNameController,
                    ),
//                    Gaps.vGap10,
                    Container(
                      padding: EdgeInsets.only(top: Dimens.gap_dp15),
                      width: double.infinity,
                      child: InkWell(
                        child: Text('忘记密码'),
                        onTap: () {
                          Util.showSnackBar(context, '请联系管理员');
                        },
                      ),
                      alignment: Alignment.centerRight,
                    ),
//                    Gaps.vGap15,
                    RoundButton(
                      text: '登录',
                      margin: EdgeInsets.only(top: 20),
                      onPressed: () {
                        _goLogin();
                      },
                    ),
                    Gaps.vGap15,
                    InkWell(
                      onTap: () {
                        NavigatorUtil.pushPage(context, RegisterPage());
                      },
                      child: Container(
                        child: RichText(
                            text: TextSpan(children: [
                          TextSpan(
                              text: '新用户？',
                              style: TextStyle(
                                  color: Colors.black54, fontSize: 15)),
                          TextSpan(
                              text: '注册',
                              style: TextStyle(
                                  color: Theme.of(context).primaryColor,
                                  fontSize: 15))
                        ])),
                      ),
                    )
                  ],
                )))
      ],
    );
  }
}
